package com.fare.taluo.core.domain.crawl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.exception.ExceptionUtils;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class ProxyIpHttpFailException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6889189986290348334L;
	private static final long E_CODE = 3002;
	private String proxyip;
	
	private static Map<String, Class<? extends Exception>> exceptions = new HashMap<String, Class<? extends Exception>>();
	static{
		exceptions.put(FailingHttpStatusCodeException.class.getName(), FailingHttpStatusCodeException.class);
		exceptions.put(org.apache.http.conn.HttpHostConnectException.class.getName(), org.apache.http.conn.HttpHostConnectException.class);
		exceptions.put(org.apache.http.TruncatedChunkException.class.getName(), org.apache.http.TruncatedChunkException.class);
		exceptions.put(org.apache.http.ConnectionClosedException.class.getName(), org.apache.http.ConnectionClosedException.class);
		exceptions.put(java.net.NoRouteToHostException.class.getName(), java.net.NoRouteToHostException.class);
		exceptions.put(org.apache.http.NoHttpResponseException.class.getName(), org.apache.http.NoHttpResponseException.class);
		exceptions.put(org.apache.http.client.ClientProtocolException.class.getName(), org.apache.http.client.ClientProtocolException.class);
	}
	public String getProxyip() {
		return proxyip;
	}

	public void setProxyip(String proxyip) {
		this.proxyip = proxyip;
	}

	public ProxyIpHttpFailException(String proxyip){
		super(String.valueOf(E_CODE));
		this.proxyip = proxyip;
	}
	
	public ProxyIpHttpFailException(int code,String proxyip){
		super(String.valueOf(code));
		this.proxyip = proxyip;
	}
	
	public ProxyIpHttpFailException(String message,String proxyip){
		super(message);
		this.proxyip = proxyip;
	}
	
	public ProxyIpHttpFailException(String message,Throwable rootCause,String proxyip){
		super(message,rootCause);
		this.proxyip = proxyip;
	}
	
	public ProxyIpHttpFailException(String message, Exception ex, String proxyip){
		super(message,ex);
		this.proxyip = proxyip;
	}
	
	public ProxyIpHttpFailException(Throwable rootCause, String proxyip){
		super(String.format("使用代理ip[%s]请求http错误!", proxyip),rootCause);
		this.proxyip = proxyip;
	}
	
	public static boolean isProxyIpException(Exception e){
		String stack = ExceptionUtils.getStackTrace(e);
		for(String cname : exceptions.keySet()){
			if(cname.equalsIgnoreCase(e.getClass().getName())  ||
					stack.indexOf(cname) > -1)
				stack = null;
				return true;
		}
		stack = null;
		return false;
	}
}
